//
//  MYStatusCellTopView.m
//  MYChatting
//
//  Created by mac on 16/8/20.
//  Copyright © 2016年 lhf. All rights reserved.
//

#import "MYStatusCellTopView.h"
#import "MYStatusPictureView.h"
#import "UIImageView+WebCache.h"
#import "User.h"
#import "Status.h"
#import "Masonry.h"


CGFloat StatusCellMarigin = 12;
CGFloat StatusCellHeadImageWidth = 35;

@interface MYStatusCellTopView ()

@property (nonatomic,weak) UIImageView *iconImage;//头像
@property (nonatomic,weak) UILabel *nameLabel;//昵称
@property (nonatomic,weak) UIImageView *mbrankImage;//会员等级图片
@property (nonatomic,weak) UIImageView *verified_type_image;//认证类型图片
@property (nonatomic,weak) UILabel *timeLabel;//发微博时间
@property (nonatomic,weak) UILabel *sourceLabel;//来源
@property (nonatomic,weak) UILabel *contentLabel;//内容

@property (nonatomic,weak) MYStatusPictureView *pictureView;//微博图片

//设置约束
@property (nonatomic,weak) MASConstraint *bottomConstraint;

@end

@implementation MYStatusCellTopView

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
        [self setUpUI];
    }
    return self;
}


//- (instancetype)initWithFrame:(CGRect)frame
//{
//    self = [super initWithFrame:frame];
//    if (self) {
//        self.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1.0];
//        [self setUpUI];
//    }
//    return self;
//}


- (void)setUpUI{
    UIImageView *iconImage = [[UIImageView alloc] init];
    [self addSubview:iconImage];
    self.iconImage = iconImage;
    
    UILabel *nameLabel = [[UILabel alloc] init];
    [self addSubview:nameLabel];
    self.nameLabel = nameLabel;
    
    UIImageView *mbrankImage = [[UIImageView alloc] init];
    [self addSubview:mbrankImage];
    self.mbrankImage = mbrankImage;
    
    UIImageView *verified_type_image = [[UIImageView alloc] init];
    [self addSubview:verified_type_image];
    self.verified_type_image = verified_type_image;
    
    UILabel *timeLabel = [[UILabel alloc] init];
    [self addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *sourceLabel = [[UILabel alloc] init];
    [self addSubview:sourceLabel];
    self.sourceLabel = sourceLabel;
    
    UILabel *contentLabel = [[UILabel alloc] init];
    contentLabel.textAlignment = NSTextAlignmentLeft;
    contentLabel.numberOfLines = 0;
    contentLabel.preferredMaxLayoutWidth = kScreenWidth - 2 *StatusCellMarigin;
    [contentLabel sizeToFit];
    [self addSubview:contentLabel];
    self.contentLabel = contentLabel;
    
    
    MYStatusPictureView *pictureView = [[MYStatusPictureView alloc] init];
    [self addSubview:pictureView];
    self.pictureView = pictureView;
    
    //设置约束
    [iconImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(StatusCellMarigin);
        make.top.equalTo(self.mas_top).offset(StatusCellMarigin);
        make.size.mas_equalTo(CGSizeMake(StatusCellHeadImageWidth, StatusCellHeadImageWidth));
    }];
    
    //UILabel不需要设置大小
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImage.mas_top);
        make.left.equalTo(iconImage.mas_right).offset(StatusCellMarigin);
    }];
    
    //加载本地图片 不需要设置图片大小了，加载网络图片了一般需要优先设置size大小
    [mbrankImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_top);
        make.left.equalTo(nameLabel.mas_right).offset(StatusCellMarigin);
    }];
    
    [verified_type_image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImage.mas_right);
        make.centerY.equalTo(iconImage.mas_bottom);
    }];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(iconImage.mas_bottom);
        make.left.equalTo(iconImage.mas_right).offset(StatusCellMarigin);
    }];
    
    [sourceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(timeLabel.mas_bottom);
        make.left.equalTo(timeLabel.mas_right).offset(StatusCellMarigin);
    }];
    
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(iconImage.mas_bottom).offset(StatusCellMarigin);
        make.left.equalTo(self.mas_left).offset(StatusCellMarigin);
    }];
    
    [pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contentLabel.mas_bottom).offset(StatusCellMarigin);
        make.left.equalTo(contentLabel.mas_left);
       // make.size.mas_equalTo(CGSizeMake(100, 100));
    }];
    
    //对顶部视图的底部设置约束
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(pictureView.mas_bottom).offset(StatusCellMarigin);
        self.bottomConstraint = make.bottom.equalTo(pictureView.mas_bottom).offset(StatusCellMarigin);;
    }];
}


- (void)setStatus:(Status *)status{
    _status = status;
    
    [self.iconImage sd_setImageWithURL:[NSURL URLWithString:status.user.profile_image_url]];
    
    self.nameLabel.text = status.user.name;
    
    if (status.user.mbrank > 0 && status.user.mbrank < 7 ) {
        self.mbrankImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"common_icon_membership_level%d",status.user.mbrank]];
    }
    
    switch (status.user.verified_type) {
        case -1:
            self.verified_type_image.image = nil;
            break;
            
        case 0:
            self.verified_type_image.image = [UIImage imageNamed:@"avatar_vip"];
            break;
            
        case 2:
            self.verified_type_image.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
            
        case 3:
            self.verified_type_image.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
            
        case 5:
            self.verified_type_image.image = [UIImage imageNamed:@"avatar_enterprise_vip"];
            break;
            
        case 220:
            self.verified_type_image.image = [UIImage imageNamed:@"avatar_grassroot"];
            break;
            
        default:
            self.verified_type_image.image = nil;
            break;
    }
    
    self.timeLabel.text = status.created_at;
    
    self.sourceLabel.text = status.source;
    
    self.contentLabel.text = status.text;
    
    self.pictureView.pic_urls = status.pic_urls;
    
    
    [self.bottomConstraint uninstall];
    //根据是否有图片来挑针约束
    if (status.pic_urls.count > 0) {
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraint = make.bottom.equalTo(self.pictureView.mas_bottom).offset(StatusCellMarigin);
        }];
    }else{
        //没配图
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            self.bottomConstraint = make.bottom.equalTo(self.contentLabel.mas_bottom).offset(StatusCellMarigin);
        }];
    }
    
}




@end
